﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FifaChamps.API.Data;

namespace FifaChamps.API.Data
{
    public static partial class DataManager
    {
        public static int Add_Tournament(string name, string venue, DateTime date, int? numberofplayers, DateTime? startdate, DateTime? enddate, string tournamentofficial)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.SP_Add_Tournament(name, venue, date, numberofplayers, startdate, enddate, tournamentofficial).First();
                return result.TournamentID.Value;
            }
        }

        public static int Update_Tournament(int tournamentID, string name, string venue, DateTime date, int? numberofplayers, DateTime? startdate, DateTime? enddate, string tournamentofficial)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.SP_Update_Tournament(tournamentID, name, venue, date, numberofplayers, startdate, enddate, tournamentofficial).First();
                return result.TournamentID.Value;
            }
        }

        public static Tournament Get_Tournament(int tournamentid) 
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.FCT_Tournaments.Where(tour => tour.ID.Equals(tournamentid)).First();
                return new Tournament(result.ID, result.TournamentName, result.Venue, result.TournamentDate.Value, result.NumberofPlayers, result.StartTime, result.EndTime, result.TournamentOfficial);
            }
        }

        public static Dictionary<int, Tournament> Get_Tournaments() 
        {
            Dictionary<int, Tournament> list = new Dictionary<int, Tournament>();
            using (var db = DataManager.FifaDB())
            {
                db.FCT_Tournaments.ToList().ForEach(tour => list.Add(tour.ID, new Tournament(tour.ID, tour.TournamentName, tour.Venue, tour.TournamentDate.Value, tour.NumberofPlayers, tour.StartTime, tour.EndTime, tour.TournamentOfficial)));
            }
            return list;
        }
    }
}
